package handler

import (
	"blog2/database"
	"blog2/util"
	"fmt"
	"strconv"

	"github.com/gin-gonic/gin"
)

type Article struct {
	ID              int    `json:"id"`
	Title           string `json:"title"`
	Content         string `json:"content"`
	PublishUserName string `json:"username"`
	Likes           int    `json:"likes"`
	Favorites       int    `json:"favorites"`
}

func PublishHTML(c *gin.Context) {
	userName := c.Query("username")
	fmt.Println("userName: ", userName)
	c.HTML(200, "publish_blog.html", gin.H{
		"username": userName,
	})
}

func PublishBlog(c *gin.Context) {
	var article Article
	if err := c.ShouldBindJSON(&article); err != nil {
		c.JSON(200, gin.H{"msg": "invalid request", "code": 400})
		return
	}
	title := article.Title
	content := article.Content
	userName := article.PublishUserName
	if len(title) == 0 || len(content) == 0 || len(userName) == 0 {
		c.JSON(200, gin.H{"msg": "invalid request", ":code": 404})
	}
	user := database.GetUserByName(userName)
	uid := user.Id
	database.CreateBlog(uid, title, content)
}

func BlogList(c *gin.Context) {
	userName := c.Query("username")
	fmt.Println("userName: ", userName)
	user := database.GetUserByName(userName)
	if user == nil {
		c.JSON(200, gin.H{"msg": "user not found", "code": 404})
		return
	}
	util.LogRus.Infof("uid: %d", user.Id)
	uid := user.Id
	blog_list, err := database.GetBlogListByUserId(uid)
	if err != nil {
		c.JSON(200, gin.H{"msg": "error getting blog list", "code": 500})
		return
	}
	articles := make([]Article, 0, len(blog_list))
	for _, blog := range blog_list {
		countMap := database.GetBlogCountFromRedis(blog.Id)
		likes, _ := countMap["L"]
		favorites, _ := countMap["F"]
		articles = append(articles, Article{ID: blog.Id,
			Title:     blog.Title,
			Content:   blog.Article,
			Likes:     likes,
			Favorites: favorites})
	}
	c.HTML(200, "blog_list.html", gin.H{
		"username": userName,
		"articles": articles,
	})
}

func GetBlogListByUid(c *gin.Context) {
	fmt.Println("cookie size: ", len(c.Request.Cookies()))
	for _, cookie := range c.Request.Cookies() {
		fmt.Println("cookie name: ", cookie.Name)
		fmt.Println("cookie value: ", cookie.Value)
	}
	var uid int = 0
	for _, cookie := range c.Request.Cookies() {
		uid, _ = strconv.Atoi(cookie.Value)
		break
	}
	blog_list, err := database.GetBlogListByUserId(uid)
	if err != nil {
		c.JSON(200, gin.H{"msg": "error getting blog list", "code": 500})
		return
	}
	articles := make([]Article, 0, len(blog_list))
	for _, blog := range blog_list {
		countMap := database.GetBlogCountFromRedis(blog.Id)
		likes, _ := countMap["L"]
		favorites, _ := countMap["F"]
		articles = append(articles, Article{ID: blog.Id,
			Title:     blog.Title,
			Content:   blog.Article,
			Likes:     likes,
			Favorites: favorites})
	}

	c.JSON(200, gin.H{"articles": articles, "code": 200})
}

func DelBlogByBlogId(c *gin.Context) {
	id := c.Param("id")
	blogId, err := strconv.Atoi(id)
	if err != nil {
		c.JSON(200, gin.H{"msg": "invalid blog id", "code": 400})
		return
	}
	database.DeleteBlogByBlogId(blogId)
	database.DelBlogCountFromRedis(blogId)
}
